<?php

include ("configs/config.php");

if ( $_SESSION['LOGIN_NAME'] <> "admin" ) 
	{
	header("Location: login-admin.php");
	exit();
	}
$tpl = new XTemplate("./template/tpl_admin.html");
$tpl->parse("main.body.menu");


if ( $site == "" )
	{
			$query_txt="SELECT url,url_site,sum(views) AS total_view,position 
					FROM redirected_links 
					GROUP BY url
					ORDER BY total_view DESC, url_site ASC";
								
			$query_all_txt="SELECT sum(views) AS all_views FROM redirected_links";
			$query_all=mysql_fetch_array(mysql_query($query_all_txt));
			$sum_all=$query_all['all_views'];
			
			$query_side_txt="SELECT sum(views) AS side_views FROM redirected_links WHERE position='1'";
			$query_side=mysql_fetch_array(mysql_query($query_side_txt));
			$sum_side=$query_side['side_views'];
			
			$query_center_txt="SELECT sum(views) AS center_views FROM redirected_links WHERE position='2'";
			$query_center=mysql_fetch_array(mysql_query($query_center_txt));
			$sum_center=$query_center['center_views'];
			
			$query_links_txt="SELECT count(*) AS counter FROM redirected_links";
			$query_links=mysql_fetch_array(mysql_query($query_links_txt));
			$sum_links=$query_links['counter'];
					
	}
else
	{
			$query_txt="SELECT url,url_site,sum(views) AS total_view,position 
					FROM redirected_links 
					WHERE url_site='$site'
					GROUP BY url
					ORDER BY views DESC, url_site ASC";

			$query_all_txt="SELECT sum(views) AS all_views FROM redirected_links WHERE url_site='$site'";
			$query_all=mysql_fetch_array(mysql_query($query_all_txt));
			$sum_all=$query_all['all_views'];
			
			$query_side_txt="SELECT sum(views) AS side_views FROM redirected_links WHERE url_site='$site' AND position='1'";
			$query_side=mysql_fetch_array(mysql_query($query_side_txt));
			$sum_side=$query_side['side_views'];
			
			$query_center_txt="SELECT sum(views) AS center_views FROM redirected_links WHERE url_site='$site' AND  position='2'";
			$query_center=mysql_fetch_array(mysql_query($query_center_txt));
			$sum_center=$query_center['center_views'];
			
			$query_links_txt="SELECT count(*) AS counter FROM redirected_links WHERE url_site='$site'";
			$query_links=mysql_fetch_array(mysql_query($query_links_txt));
			$sum_links=$query_links['counter'];
	}				
$query=mysql_query($query_txt);




//select sum(views) from redirected_links where position='1';

$counter=1;
while ( $line = mysql_fetch_array($query) )
	{
	$tpl->assign("counter",$counter);
	$tpl->assign("site",$line['url_site']);
	$tpl->assign("views",$line['total_view']);
	$tpl->assign("url",$line['url']);
	
	if ( $line['position'] == 1 )
			{
			$tpl->assign("position","Side links");			
			}
	else
			{
			$tpl->assign("position","Central links");						
			}		

	
	$counter++;
	$tpl->parse("main.body.links_by_url.row");
	}		
		
	$tpl->assign("total_clicks",$sum_all);
	$tpl->assign("side_clicks",$sum_side);	
	$tpl->assign("center_clicks",$sum_center);
	$tpl->assign("sum_links",$sum_links);	

$tpl->parse("main.body.links_by_url");
$tpl->parse("main.body");

$tpl->parse("main");
$tpl->out("main");
?>
